//
//  ProductViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/3/29.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ProductViewController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#import "OnLineProductModel.h"
#import "ProductPackageModel.h"
#import "ProductDetailViewController.h"
#import "OnLineProductTableViewCell.h"
#import "ProductPackageTableViewCell.h"
#import "SearchProductViewController.h"
#import "OnLineProductViewController.h"
#import "ZGWWebViewController.h"
#import "ZGWNewOnLineProductVersionTableViewCell.h"

//梅江林
#import "MyOrderViewController.h"
#import "ShowDaySignInViewController.h"


#define kProductData @"http://120.26.129.84/api/production/all"

@interface ProductViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *productTableView;
@property (nonatomic, strong) NSMutableArray *onLineProductArray; // 上线产品的数据源
@property (nonatomic, strong) NSMutableArray *produceLeiArray; // 产品类数据源
@property (nonatomic, strong) NSTimer *myTimer; // 检查请求是否超时
@property (nonatomic, assign) NSInteger numberOfTime; // 存储请求时间

@property (nonatomic, assign) BOOL isShowWeiShangChengButton; // 是否显示微商城按钮

@property (nonatomic, assign) BOOL showProduct;

@property (nonatomic, strong) UILabel *tempLabel;

//梅江林
@property(nonatomic,assign)BOOL gotoShowDaySignInViewController;
@end

@implementation ProductViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = NO;
    
    
    //梅江林
    self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
    self.navigationController.navigationBar.hidden=NO;
    if (!_gotoShowDaySignInViewController) {
        [self getSuperSignInHttpData];
    }else{
        //签到失败，开始准备后一天的签到
        NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
        NSString *lastSignTime=[user objectForKey:@"lastSignTime"];
        if (lastSignTime==nil) {
            return;
        }
        NSInteger days=[self getDays:lastSignTime];
        if (days>0) {
            [self getSuperSignInHttpData];
        }else{
            
        }
        
    }

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    // 关闭动画
    [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
    // 关闭计时器
    [self.myTimer invalidate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//      self.isShowWeiShangChengButton = YES;
//    self.showProduct = YES;
    
    [self changePage];
    // 布局
    [self addMySubview];
    
    if ([kUserNewVerson isEqualToString:@"YES"]) {
        // 使用新版本
        // 布局
        [self addMySubview];
    } else if ([kUserNewVerson isEqualToString:@"NO"]) {
        // 旧版本
        // 布局
        [self addMySubview];
        // 检查网络并添加数据源
        [self searchNetAndAddData];
    }
}

- (void)changePage
{
    self.navigationItem.title = @"产品";
    [self.navigationController.navigationBar setTranslucent:NO];
    // iOS7.0以后需要这样设置
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:22]};
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1]];
    
    if (self.isJump == YES) {
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        leftImageView.image = [UIImage imageNamed:@"houtui"];
        UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
        [leftImageView addGestureRecognizer:leftTapGes];
        UIBarButtonItem *leftFirstItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
        self.navigationItem.leftBarButtonItem = leftFirstItem;
    }
    
    if (self.isShowWeiShangChengButton == NO) {
        UIImageView *rightImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        rightImageV.image = [UIImage imageNamed:@"sousuo-chanpin"];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightImageV];
        
        UITapGestureRecognizer *rightTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(search:)];
        [rightImageV addGestureRecognizer:rightTapGes];
    } else {
        UIImageView *rightImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        rightImageV.image = [UIImage imageNamed:@"sousuo-chanpin"];
        UIBarButtonItem *firstRightItem = [[UIBarButtonItem alloc] initWithCustomView:rightImageV];
        // 微商城
        UIBarButtonItem *secondRightItem = [[UIBarButtonItem alloc] initWithTitle:@"微商城" style:UIBarButtonItemStyleDone target:self action:@selector(weiShangCheng:)];
        
        self.navigationItem.rightBarButtonItems = @[firstRightItem, secondRightItem];
        
        UITapGestureRecognizer *rightTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(search:)];
        [rightImageV addGestureRecognizer:rightTapGes];
    }
    
  
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 搜索
- (void)search:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"搜索");
    SearchProductViewController *searchProductVC = [[SearchProductViewController alloc] init];
    [self.navigationController pushViewController:searchProductVC animated:YES];
}

#pragma mark - 微商城
- (void)weiShangCheng:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"微商城");
//    ZGWWebViewController *zgwWebVC = [[ZGWWebViewController alloc] init];
//    zgwWebVC.httpUrlString = @"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5dedab302d5ac1a5&redirect_uri=http%3A%2F%2Fcms.chaomar.com%2Findex.php%3Fg%3DWap%26m%3DIndex%26a%3Dindex%26token%3Dtvyuws1466140499%26diymenu%3D1&response_type=code&scope=snsapi_base&state=#wechat_redirect";
    
//    [self.navigationController pushViewController:zgwWebVC animated:YES];
    self.showProduct = !self.showProduct;
    if (self.showProduct == YES) {
        [self.view bringSubviewToFront:self.productTableView];
    } else if (self.showProduct == NO) {
        [self.view bringSubviewToFront:self.tempLabel];
    }
    

}

#pragma mark - 布局
- (void)addMySubview
{
//    self.tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
//    self.tempLabel.backgroundColor = [UIColor redColor];
//    [self.view addSubview:_tempLabel];
//    
    
    self.productTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 49) style:UITableViewStylePlain];
    self.productTableView.backgroundColor = [UIColor whiteColor];
    self.productTableView.showsVerticalScrollIndicator = NO;
    self.productTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.productTableView.delegate = self;
    self.productTableView.dataSource = self;
    [self.view addSubview:_productTableView];
    


    
}

#pragma mark - 检查网络并建立数据源
- (void)searchNetAndAddData
{
    self.productTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if ([[[NetStatusHandle shareHandle] getNetStatus] isEqualToString:@"YES"]) {
            // 说明有网络
            // 建立数据源
            [self addData];
            // 建立计时器，计算是否超时
            self.myTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(goTimeOnProduct:) userInfo:nil repeats:YES];

            [[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSRunLoopCommonModes];
            self.numberOfTime = 0;
            
            // 开始动画
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] startAnimation];
        } else {
            // 没网络
            NSLog(@"没网络");
            // 添加提示框
            [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kNoNetRefresh ToView:self];
            // 停止刷新
            [self.productTableView.header endRefreshing];
        }
        
    }];
    [self.productTableView.header beginRefreshing];
}

#pragma mark - 计时器方法
- (void)goTimeOnProduct:(NSTimer *)timer
{
    self.numberOfTime++;
    NSLog(@"产品页时间= %ld", self.numberOfTime);
    if (self.numberOfTime > 10) {
        NSLog(@"产品页请求超时");
        self.numberOfTime = 0;
        [self.myTimer invalidate];
        // 关闭刷新
        [self.productTableView.header endRefreshing];
        // 关闭动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kQingQiuChaoShiReFresh ToView:self];
    }
}

#pragma mark - 数据源
- (void)addData
{
   
    
    

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:kProductData parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (!responseObject) {
            return;
        }
        // 关闭刷新
                [self.productTableView.header endRefreshing];
                // 将计时器置为0
                self.numberOfTime = 0;
                [self.myTimer invalidate];
                // 结束动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
        
                // 解析数据
                self.onLineProductArray = [NSMutableArray array];
                self.produceLeiArray = [NSMutableArray array];

        // 上线产品数组
        NSArray *productionArray = [responseObject valueForKey:@"productions"];
        for (NSDictionary *tempDic in productionArray) {
            OnLineProductModel *onLineModel = [[OnLineProductModel alloc] init];
            [onLineModel setValuesForKeysWithDictionary:tempDic];
            [self.onLineProductArray addObject:onLineModel];
        }
        // 类数组
        NSArray *classificationsArray = [responseObject valueForKey:@"classifications"];
        for (NSDictionary *tempDic in classificationsArray) {
//            NSString *leiName = [tempDic valueForKey:@"name"];
//            if ([leiName isEqualToString:@"心脑血管疾病"] || [leiName isEqualToString:@"免疫性疾病"] || [leiName isEqualToString:@"肿瘤"]||[leiName isEqualToString:@"精神类疾病"]) {
//                ProductPackageModel *productLeiModel = [[ProductPackageModel alloc] init];
//                [productLeiModel setValuesForKeysWithDictionary:tempDic];
//                [self.produceLeiArray addObject:productLeiModel];
//            }
            ProductPackageModel *productLeiModel = [[ProductPackageModel alloc] init];
            [productLeiModel setValuesForKeysWithDictionary:tempDic];
            [self.produceLeiArray addObject:productLeiModel];
        }
//        NSLog(@"上线产品数据源:%@", self.onLineProductArray);
//        NSLog(@"类数据源:%@", self.produceLeiArray);
        
        [self.productTableView reloadData];
        
        if (self.shouldJumpLei == YES) {
            // 跳到类的分区
            [self.productTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] animated:YES scrollPosition:UITableViewScrollPositionTop];
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        // 关闭动画
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] stopAnimation];
        // 添加提示框
        [[JiaZaiAnimationHandle shareJiaZaiAnimationHandle] addAlertTitle:@"通知" Content:kHuoQuDataShiBaiRefresh ToView:self];
        // 停止刷新
        [self.productTableView.header endRefreshing];
        // 暂停计时器
        [self.myTimer invalidate];
        self.numberOfTime = 0;
    }];
}

#pragma mark - 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([kUserNewVerson isEqualToString:@"YES"]) {
        if (section == 0) {
            return 3;
            
        }
        // 说明是产品包
        if (section == 1) {
            return 3;
        }
    } else if ([kUserNewVerson isEqualToString:@"NO"]) {
        if (section == 0) {
            // 说明是上线产品
            if (self.onLineProductArray.count == 0) {
                return 0;
            } else {
                return self.onLineProductArray.count;
            }
            
        }
        // 说明是产品包
        if (self.produceLeiArray.count == 0) {
            return 0;
        } else {
            return self.produceLeiArray.count;
        }
    }
    return 0;
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([kUserNewVerson isEqualToString:@"YES"]) {
        if (indexPath.section == 0) {
            ZGWNewOnLineProductVersionTableViewCell *cell = [[ZGWNewOnLineProductVersionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        } else if (indexPath.section == 1) {
            static NSString *identifier = @"productPackage";
            ProductPackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[ProductPackageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    } else if ([kUserNewVerson isEqualToString:@"NO"]) {
        if (indexPath.section == 0) {
            // 说明是上线产品
            static NSString *identifier = @"onLineProduct";
            OnLineProductTableViewCell *cell = [[OnLineProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (indexPath.section == 0) {
                if (self.onLineProductArray.count  > indexPath.row) {
                    cell.model = self.onLineProductArray[indexPath.row];
                }
            }
            [cell.joinDingDanButton addTarget:self action:@selector(clickOnLineCell:) forControlEvents:UIControlEventTouchUpInside];
            cell.joinDingDanButton.tag = 200;
            [cell.buyNowButton addTarget:self action:@selector(clickOnLineCell:) forControlEvents:UIControlEventTouchUpInside];
            cell.buyNowButton.tag = 201;
            
            return cell;
        }
        // 说明是产品包
        static NSString *identifier = @"productPackage";
        ProductPackageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[ProductPackageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.section == 1) {
            if (self.produceLeiArray.count > indexPath.row) {
                cell.model = self.produceLeiArray[indexPath.row];
            }
        }
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([kUserNewVerson isEqualToString:@"YES"]) {
        if (indexPath.section == 0) {
            return 225 * kAdjustParamater;
        } else if (indexPath.section == 1) {
            // 说明是类
            ProductPackageTableViewCell *cellTwo = (ProductPackageTableViewCell *)[self tableView:_productTableView cellForRowAtIndexPath:indexPath];
            return cellTwo.grayLineOnBottom.bottom;
        }
    } else if ([kUserNewVerson isEqualToString:@"NO"]) {
        if (indexPath.section == 0) {
            OnLineProductTableViewCell *cell = (OnLineProductTableViewCell *)[self tableView:_productTableView cellForRowAtIndexPath:indexPath];
            //  NSLog(@"底部 = %f", cell.grayLineOnCellBottom.bottom);
            return cell.grayLineOnCellBottom.bottom;
        }
        // 说明是类
        ProductPackageTableViewCell *cellTwo = (ProductPackageTableViewCell *)[self tableView:_productTableView cellForRowAtIndexPath:indexPath];
        return cellTwo.grayLineOnBottom.bottom;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *whiteBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 45 * kAdjustParamater)];
    whiteBackGroundView.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 11 * kAdjustParamater, 150 * kAdjustParamater, 20 * kAdjustParamater)];
 //   titleLabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    titleLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    titleLabel.font = [UIFont systemFontOfSize:k20Big];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    if (section == 0) {
        titleLabel.text = @"上线产品";
    } else {
        titleLabel.text = @"检测大类";
    }
    [whiteBackGroundView addSubview:titleLabel];
    // 灰色条
    UILabel *grayLineOnBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, 43 * kAdjustParamater, kScreenWidth, 1.5 * kAdjustParamater)];
    grayLineOnBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [whiteBackGroundView addSubview:grayLineOnBottom];
    return whiteBackGroundView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45  * kAdjustParamater;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([kUserNewVerson isEqualToString:@"YES"]) {
        if (indexPath.section == 0) {
            ZGWWebViewController *zgwWebVC = [[ZGWWebViewController alloc] init];
            zgwWebVC.httpUrlString = @"12";
            zgwWebVC.isShowBuybutton = YES;
            [self.navigationController pushViewController:zgwWebVC animated:YES];
        } else if (indexPath.section == 1) {
            
        }

    } else if ([kUserNewVerson isEqualToString:@"NO"]) {
        if (indexPath.section == 0) {
            OnLineProductViewController *onLineProductVC = [[OnLineProductViewController alloc] init];
            if (self.onLineProductArray.count > indexPath.row) {
                OnLineProductModel *model = self.onLineProductArray[indexPath.row];
                onLineProductVC.onLineProductName = model.name;
                onLineProductVC.onLineProductID = [NSString stringWithFormat:@"%@", model.myID];
            }
            [self.navigationController pushViewController:onLineProductVC animated:YES];
        }
        
        if (indexPath.section == 1) {
            // 获取产品包的model
            ProductDetailViewController *productDetailVC = [[ProductDetailViewController alloc] init];
            if (self.produceLeiArray.count > indexPath.row) {
                ProductPackageModel *model = self.produceLeiArray[indexPath.row];
                productDetailVC.leiIDString = [NSString stringWithFormat:@"%@", model.myLeiID];
            }
            [self.navigationController pushViewController:productDetailVC animated:YES];
        }
    }
    
}

#pragma mark - 点击加入订单或者立即购买
- (void)clickOnLineCell:(UIButton *)button
{
    UIView *contentView = [button superview];
    OnLineProductTableViewCell *tempCell = (OnLineProductTableViewCell *)[contentView superview];
    NSIndexPath *tempIndexPath = [self.productTableView indexPathForCell:tempCell];
    switch (button.tag) {
        case 200:
        {
            NSLog(@"加入订单,section = %ld,row = %ld",tempIndexPath.section, tempIndexPath.row);
            break;
        }
            case 201:
        {
            NSLog(@"立即购买,section = %ld,row = %ld",tempIndexPath.section, tempIndexPath.row);
            OnLineProductModel *model = self.onLineProductArray[tempIndexPath.row];
            MyOrderViewController *vc=[MyOrderViewController new];
            vc.model=model;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        default:
            break;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat height = scrollView.frame.size.height;
    CGFloat contentYOffset = scrollView.contentOffset.y;
    CGFloat distanceFromBottom = scrollView.contentSize.height - contentYOffset;
    if (distanceFromBottom < height) {
        NSLog(@"底部");
        self.productTableView.bounces = NO;
    }
    if (contentYOffset == 0) {
        NSLog(@"顶部");
        self.productTableView.bounces = YES;
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//梅江林
-(void)getSuperSignInHttpData
{
    AFHTTPRequestOperationManager * manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token= [user objectForKey:@"token"];
    
    [manager GET:[NSString stringWithFormat:@"%@%@",UserInfo,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"userInfo===%@",dic);
        if (dic) {
            if ([dic[@"last_sign"] isKindOfClass:[NSNull class]]) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    ShowDaySignInViewController *vc=[ShowDaySignInViewController new];
                    vc.days=[NSString stringWithFormat:@"%@",dic[@"all_sign"]];
                    vc.dontGotoShowDaySignInViewController=^(BOOL isGo){
                        _gotoShowDaySignInViewController=isGo;
                    };
                    [self.navigationController pushViewController:vc animated:YES];
                });
                
            }else{
                NSString *str=[NSString stringWithFormat:@"%@",dic[@"last_sign"]];
                NSInteger days=[self getDays:str];
                if (days>0) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        ShowDaySignInViewController *vc=[ShowDaySignInViewController new];
                        vc.days=[NSString stringWithFormat:@"%@",dic[@"all_sign"]];
                        vc.dontGotoShowDaySignInViewController=^(BOOL isGo){
                            _gotoShowDaySignInViewController=isGo;
                        };
                        [self.navigationController pushViewController:vc animated:YES];
                    });
                }else{
                    
                }
                
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    
    
}

-(NSInteger)getDays:(NSString *)str
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setFirstWeekday:2];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *fromDate;
    NSDate *toDate;
    [gregorian rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:[dateFormatter dateFromString:str]];
    [gregorian rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:[NSDate date]];
    NSDateComponents *dayComponents = [gregorian components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];
    // dayComponents.day  即为间隔的天数
    NSLog(@"======%ld",dayComponents.day);
    return dayComponents.day;
}









































@end
